package com.kexio.scheduler.enums;

/**
 * 任务状态枚举
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public enum JobStatus {
    
    /**
     * 等待执行
     */
    WAITING("WAITING", "等待执行"),
    
    /**
     * 正在执行
     */
    RUNNING("RUNNING", "正在执行"),
    
    /**
     * 执行成功
     */
    SUCCESS("SUCCESS", "执行成功"),
    
    /**
     * 执行失败
     */
    FAILED("FAILED", "执行失败"),
    
    /**
     * 已暂停
     */
    PAUSED("PAUSED", "已暂停"),
    
    /**
     * 已停止
     */
    STOPPED("STOPPED", "已停止"),
    
    /**
     * 已取消
     */
    CANCELLED("CANCELLED", "已取消");
    
    private final String code;
    private final String description;
    
    JobStatus(String code, String description) {
        this.code = code;
        this.description = description;
    }
    
    public String getCode() {
        return code;
    }
    
    public String getDescription() {
        return description;
    }
    
    /**
     * 根据代码获取枚举
     */
    public static JobStatus fromCode(String code) {
        for (JobStatus status : values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        throw new IllegalArgumentException("Unknown job status code: " + code);
    }
    
    /**
     * 是否为运行状态
     */
    public boolean isRunning() {
        return this == RUNNING;
    }
    
    /**
     * 是否为最终状态
     */
    public boolean isFinalStatus() {
        return this == SUCCESS || this == FAILED || this == CANCELLED;
    }
}
